package com.evan.review.architecture.mvc.controller;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.evan.review.R;
import com.evan.review.architecture.mvc.model.TodoItem;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 待办事项适配器
 * MVC架构中的Controller层的一部分，负责处理View和Model之间的交互
 */
public class TodoAdapter extends RecyclerView.Adapter<TodoAdapter.TodoViewHolder> {
    
    private final List<TodoItem> todoItems;
    private final Context context;
    private final TodoItemClickListener listener;
    
    public interface TodoItemClickListener {
        void onItemClick(TodoItem todoItem);
        void onItemCheckChanged(TodoItem todoItem, boolean isChecked);
        void onItemDeleteClick(TodoItem todoItem);
    }
    
    public TodoAdapter(Context context, List<TodoItem> todoItems, TodoItemClickListener listener) {
        this.context = context;
        this.todoItems = todoItems;
        this.listener = listener;
    }
    
    @NonNull
    @Override
    public TodoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_todo_mvc, parent, false);
        return new TodoViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull TodoViewHolder holder, int position) {
        TodoItem todoItem = todoItems.get(position);
        holder.bind(todoItem);
    }
    
    @Override
    public int getItemCount() {
        return todoItems.size();
    }
    
    public void updateData(List<TodoItem> newItems) {
        todoItems.clear();
        todoItems.addAll(newItems);
        notifyDataSetChanged();
    }
    
    class TodoViewHolder extends RecyclerView.ViewHolder {
        private final TextView tvTitle;
        private final TextView tvDescription;
        private final TextView tvDueDate;
        private final TextView tvPriority;
        private final CheckBox cbCompleted;
        private final ImageButton btnDelete;
        
        public TodoViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_todo_title);
            tvDescription = itemView.findViewById(R.id.tv_todo_description);
            tvDueDate = itemView.findViewById(R.id.tv_todo_due_date);
            tvPriority = itemView.findViewById(R.id.tv_todo_priority);
            cbCompleted = itemView.findViewById(R.id.cb_todo_completed);
            btnDelete = itemView.findViewById(R.id.btn_todo_delete);
        }
        
        public void bind(final TodoItem todoItem) {
            tvTitle.setText(todoItem.getTitle());
            tvDescription.setText(todoItem.getDescription());
            
            // 格式化日期
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            tvDueDate.setText(String.format("截止日期: %s", dateFormat.format(new Date(todoItem.getDueDate()))));
            
            // 设置优先级
            String priority;
            switch (todoItem.getPriority()) {
                case 1:
                    priority = "低";
                    tvPriority.setBackgroundResource(R.drawable.bg_priority_low);
                    break;
                case 2:
                    priority = "中";
                    tvPriority.setBackgroundResource(R.drawable.bg_priority_medium);
                    break;
                case 3:
                    priority = "高";
                    tvPriority.setBackgroundResource(R.drawable.bg_priority_high);
                    break;
                default:
                    priority = "未知";
                    break;
            }
            tvPriority.setText(String.format("优先级: %s", priority));
            
            // 设置完成状态
            cbCompleted.setChecked(todoItem.isCompleted());
            
            // 设置点击事件
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onItemClick(todoItem);
                }
            });
            
            cbCompleted.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (listener != null && buttonView.isPressed()) { // 确保是用户操作触发的
                    listener.onItemCheckChanged(todoItem, isChecked);
                }
            });
            
            btnDelete.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onItemDeleteClick(todoItem);
                }
            });
        }
    }
} 